package com.xtiim.battery.discharge.event;

import cn.hutool.core.collection.CollectionUtil;

import java.util.List;

public class DefaultEventPublisher implements EventPublisher {

    private static DefaultEventPublisher instance = new DefaultEventPublisher();

    private EventMulticaster eventMulticaster = DefaultEventMulticaster.getInstance();

    private DefaultEventPublisher() {}

    public static DefaultEventPublisher getInstance() {
        return instance;
    }

    @Override
    public void publishEvent(ApplicationEvent event) {
        List<ApplicationListener> listeners = eventMulticaster.getListeners(event.getClass());
        if (CollectionUtil.isEmpty(listeners)) {
            return;
        }

        for (ApplicationListener listener : listeners) {
            listener.onApplicationEvent(event);
        }
    }
}
